local MainScene = class("MainScene", cc.load("mvc").ViewBase)

MainScene.RESOURCE_FILENAME = "scene/MainScene.csb"

local fu = cc.FileUtils:getInstance()

function MainScene:onCreate()
    self:initUI()
    self:initData()
end

function MainScene:initUI()
    cc.SimpleAudioEngine:getInstance():playBackgroundMusic("sounds/bg.mp3", true)

    local root = self:getResourceNode()
    root:setAnchorPoint(cc.p(0.5, 0.5))
    root:setPosition(win_size.width / 2, win_size.height / 2)

    self._normal_mode_btn = root:getChildByName("normal_btn")
    self._normal_mode_btn:addTouchEventListener(
        function(sender, type)
            if type == ccui.TouchEventType.ended then
                local scene = self:getApp():createScene("GameScene")
                cc.Director:getInstance():replaceScene(scene)
            end
        end
    )

    self._mini_mode_btn = root:getChildByName("mini_btn")

    self._quiz_mode_btn = root:getChildByName("quiz_btn")

    self._survival_mode_btn = root:getChildByName("survival_btn")

    self._gate_text = root:getChildByName("gate_text")
    self._sub_gate_text = root:getChildByName("sub_gate_text")

    self._achieve_btn = root:getChildByName("achieve_btn")
    self._achieve_btn:addTouchEventListener(
        function(sender, type)
            if type == ccui.TouchEventType.ended then
                ck.gate = 1
                ck.sub_gate = 1
                ck.is_new_gate = true
                self._gate_text:setString(ck.gate)
                self._sub_gate_text:setString(ck.sub_gate)
            end
        end
    )

    self._chip_btn = root:getChildByName("chip_btn")

    self._exit_btn = root:getChildByName("exit_btn")
    self._exit_btn:addTouchEventListener(
        function(sender, type)
            if type == ccui.TouchEventType.ended then
                local f = require("app.utils.Table2String")
                local s = "return " .. f(ck)

                f = io.open(fu:fullPathFromRelativeFile("checkpoint.lua", fu:getWritablePath()), "w")
                f:write(s)
                f:close()

                cc.Director:getInstance():endToLua()
            end
        end
    )

    self._set_btn = root:getChildByName("set_btn")
    self._set_btn:addTouchEventListener(
        function(sender, type)
            if type == ccui.TouchEventType.ended then
                self._set_layer:setVisible(true)
            end
        end
    )

    self._set_layer = root:getChildByName("set_layer")
    self._set_layer:setVisible(false)

    local ok_btn = self._set_layer:getChildByName("ok_btn")
    ok_btn:setTitleText(vm["back"])
    ok_btn:setVisible(true)
    ok_btn:addTouchEventListener(
        function(sender, type)
            if type == ccui.TouchEventType.ended then
                self._set_layer:setVisible(false)
            end
        end
    )

    local music_slider = self._set_layer:getChildByName("music_slider")
    music_slider:setPercent(cc.SimpleAudioEngine:getInstance():getMusicVolume() * 100)
    music_slider:addEventListener(
        function(sender, type)
            if type == ccui.SliderEventType.percentChanged then
                cc.SimpleAudioEngine:getInstance():setMusicVolume(music_slider:getPercent() / 100)
            end
        end
    )

    local sound_slider = self._set_layer:getChildByName("sound_slider")
    sound_slider:setPercent(cc.SimpleAudioEngine:getInstance():getEffectsVolume() * 100)
    sound_slider:addEventListener(
        function(sender, type)
            if type == ccui.SliderEventType.percentChanged then
                cc.SimpleAudioEngine:getInstance():setEffectsVolume(sound_slider:getPercent() / 100)
            end
        end
    )
end

function MainScene:initData()
    if ck == nil then
        local file = io.open(fu:fullPathFromRelativeFile("checkpoint.lua", fu:getWritablePath()), "r")
        if file then
            local str = file:read("*all")
            cc.exports.ck = loadstring(str)()
        else
            cc.exports.ck = {}
            ck.gate = 1
            ck.sub_gate = 1
            ck.is_new_gate = true
        end
    end
    self._gate_text:setString(ck.gate)
    self._sub_gate_text:setString(ck.sub_gate)
end

return MainScene
